package com.eldenring._04_oneway;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception {
        // 1.创建消费者
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("oneway_group_producer");
        // 2.创建连接 127.0.0.1:9876(这是nameServer的url地址)
        consumer.setNamesrvAddr("127.0.0.1:9876");
        // 3.订阅，表示此消费者消费从那个主题的消息，二级分类可以是*
        consumer.subscribe("oneway","group");
        // 4.开启监听
        consumer.registerMessageListener((List<MessageExt> list, ConsumeConcurrentlyContext context)->{
            for (MessageExt message : list) {
                // message.getBody() 在消息体中获取消息，将消息转换为字符串，设置字符集为UTF-8
                System.out.println(new String(message.getBody(), StandardCharsets.UTF_8));
            }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        );
        // 5.启动消费者
        consumer.start();
    }


}
